#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: V1DagDataSync.py 
@version:
@time: 2022/03/23 
"""
from TapClientCore.T04_logic.Dag02.relations.relation import *


class V1DagDataSync:

    def __init__(self):
        # dag
        self.init_source = None
        self.last_target = None
        self.join = None
        self.last_target_relation = None
        self.edges = []
        self.nodes = []
        from TapClientCore.T03_api_body_templates.v2_api_body import TASK_CREATE_DATA_SYNC_DAG
        self.dag = TASK_CREATE_DATA_SYNC_DAG.copy()

    def edge(self, source, sink, relation: [Association, MergeAssociation], join):
        # edge
        this_edge = {
            'source': source.node.get('id'),
            'target': sink.node.get('id')
        }
        # edge add
        self.edges.append(this_edge)
        if not join:
            if relation:
                self.init_source.node.update(relation.source)
                sink.node.update(relation.sink)
            # 如果已经又了目标node，那就不用再添加此时的source
            if self.last_target:
                self.nodes.append(sink.node)
            else:
                self.nodes.append(source.node)
                self.nodes.append(sink.node)
            self.last_target = sink
        else:
            if relation:
                self.init_source.node.update(relation.source)
            if join == 'left':
                sink.node.update({
                    'leftNodeId': source.node.get('id'),
                })
                self.nodes.append(source.node)
                self.nodes.append(sink.node)
                self.join = sink
            if join == 'right':
                self.join.node.update({'rightNodeId': source.node.get('id')})

        # dag and edge set
        self.dag['edges'] = self.edges
        self.dag['nodes'] = self.nodes

    def merge(self, source, sink, relation: [MergeAsOne, MergeAsObject, MergeAsList]):
        # edge
        this_edge = {
            'source': source.node.get('id'),
            'target': sink.node.get('id')
        }
        self.last_target = sink
        # edge add
        self.edges.append(this_edge)
        # node add
        # init_sourceId获取
        relation.merge.update({'id': self.init_source.node.get('id')})
        # sink node mergeProperties append relation
        self.init_source.node.update(relation.source)
        if type(relation) == MergeAsOne:
            sink.node['mergeProperties'].append(relation.merge)
            self.nodes.append(source.node)
            self.nodes.append(sink.node)
            self.last_target_relation = relation
        if type(relation) == MergeAsObject:
            self.last_target_relation.merge['children'].append(relation.merge)
            self.nodes.append(self.init_source.node)
        if type(relation) == MergeAsList:
            self.last_target_relation.merge['children'].append(relation.merge)
            self.nodes.append(self.init_source.node)
            self.last_target_relation = relation
        if type(relation) == MergeListAsObject:
            self.last_target_relation.merge['children'].append(relation.merge)
            self.nodes.append(self.init_source.node)
        # dag and edge set
        self.dag['edges'] = self.edges
        self.dag['nodes'] = self.nodes
